package com.dragonsoftbravo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.dragonsoftbravo.utils.azure.AzureConstants;
import cn.com.dragonsoftbravo.utils.azure.factory.BlobContainerFactory;
import cn.com.dragonsoftbravo.utils.azure.factory.BlobTableFactory;
import cn.com.dragonsoftbravo.utils.azure.model.LogsForUpload;

import com.microsoft.azure.storage.blob.CloudBlobDirectory;
import com.microsoft.azure.storage.table.CloudTable;
import com.microsoft.azure.storage.table.TableQuery;

@Controller
@RequestMapping("/adminreport")
public class ReportController {

	@RequestMapping(value = "/pass")
	public String adminreport(Model model) {
		return "report/pass";
	}

	@RequestMapping(value = "/reportdetail")
	public String reportdetail(Model model, String password) {
		try {
			if (password.equals("SonyAlphaCafeReport")) {

				CloudTable tableLog = BlobTableFactory.getTable(AzureConstants.TABLE_NAME_LOGS_UPLOAD);
				CloudBlobDirectory originalDir = BlobContainerFactory.getOriginailDirectory();
				CloudBlobDirectory thumbnailDir = BlobContainerFactory.getThumbnailDirectory();

				Iterable<LogsForUpload> logs = tableLog.execute(TableQuery.from(LogsForUpload.class));
				List<LogsForUpload> logList = new ArrayList<LogsForUpload>();

				for (LogsForUpload log : logs) {
					logList.add(log);
				}

				int originalCount = originalDir.listBlobsSegmented().getLength();
				int thumbnailCount = thumbnailDir.listBlobsSegmented().getLength();

				model.addAttribute("originalCount", originalCount);
				model.addAttribute("thumbnailCount", thumbnailCount);
				model.addAttribute("totalCount", originalCount + thumbnailCount);
				model.addAttribute("logList", logList);
				
				return "report/reportdetail";
			}

			return "report/pass";
		} catch (Exception ex) {
			return "report/pass";
		}
	}

}
